package com.tongcao.cn.contoller;

import com.google.code.kaptcha.Producer;
import com.tongcao.cn.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @Author:tongcao
 * @Date:2018/8/28 16:47
 * Description:
 */
@Controller
@RequestMapping("/sys")
@Api(value = "/sys",description = "系统登录")
public class SysLoginController {

    @Resource
    private Producer producer;

    @SysLog("生产验证码")
    @ApiOperation(value = "/captcha",notes = "生产验证码")
    @RequestMapping("/captcha")
    public void captcha(HttpServletResponse response) throws IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        //生成文字验证码
        String text = producer.createText();
        //生成图片验证码
        BufferedImage image = producer.createImage(text);
        //保存到shiro session
//        ShiroUtils.setSessionAttribute(Constants.KAPTCHA_SESSION_KEY, text);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
    }
}
